/*
 * @(#)ShareWeiBo.java	 2012-3-15
 *
 * Copyright 2004-2012 WXXR Network Technology Co. Ltd. 
 * All rights reserved.
 * 
 * WXXR PROPRIETARY/CONFIDENTIAL.
 */

package com.ykdl.member.kid.gears;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ykdl.member.kid.R;
import com.ykdl.member.base.QLog;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.util.MThumbnailPhoto;
import com.ykdl.member.kid.util.WeiBoUtil;
/**
 * 
 * 微博分享的编辑页面
 *
 * 
 * @see
 * @since 1.0
 */

public class ShareWeiBo extends BaseScreen {
	//String strpath = Gallery3DActivity.path, cotent;
	ImageView weibo_pic;
	EditText weibo_word;
	TextView weibo_wordnum;
	String tip="";
	String picpath="";
	String share_rowid="";
	private RelativeLayout top_nav;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setTopNav(R.string.share_dialog_title, R.string.back, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		}, R.string.send_sina, new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				send();
			}
		}, 0);
		
		setContent(R.layout.shareweibo);
		tip=this.getIntent().getStringExtra(KidAction.SHARE_TIP);
		picpath=this.getIntent().getStringExtra(KidAction.SHARE_PICPATH);
		share_rowid=this.getIntent().getStringExtra(KidAction.SHARE_ROWID);
		
		if(tip==null){
			tip="";
		}
		if(picpath==null){
			picpath="";
		}
		if(!picpath.endsWith("s.jpg")){
		String  temppath=picpath.replace(".jpg", "s.jpg");
		File f=new File(temppath);
		if(f.exists()){
			picpath=temppath;
		}
		}
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		findViewByIds();
		if(share_rowid!=null){
			top_nav.setBackgroundResource(R.drawable.title_bg);
			mLeft.setBackgroundResource(R.drawable.title_bg);
			mLeft.setTextSize(16);
			mLeft.setTextColor(getResources().getColorStateList(R.color.footprint_titlebut));
			mRight.setBackgroundResource(R.drawable.right_button);
			mRight.setTextSize(16);
			mRight.setTextColor(getResources().getColorStateList(
					R.color.footprint_titlebut));
			mTitle.setClickable(false);
			mTitle.setTextColor(Color.WHITE);
			mTitle.setShadowLayer(3.0f, 0, 0, Color.BLACK);
		}
		init();
	}

	private void findViewByIds(){
		top_nav = (RelativeLayout) findViewById(R.id.set_downbut);
		weibo_pic = (ImageView) findViewById(R.id.weibo_pic);
		weibo_pic.setImageBitmap(MThumbnailPhoto.lessBitmapPath(picpath, 35, 35));
		weibo_wordnum = (TextView) findViewById(R.id.weibo_wordnum);
		weibo_word = (EditText) findViewById(R.id.weibo_word);
		weibo_word.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}

			@Override
			public void afterTextChanged(Editable s) {
				
				String text = weibo_wordnum.getText().toString();
				int numNew = -1;
				String limit = null;
				try {
					numNew = Integer.valueOf(text.split("/")[0]);
					limit = "/" + text.split("/")[1];
				} catch (Exception e) {
					QLog.error(e.toString());
				}
				if (numNew != -1 && limit != null) {
					numNew = weibo_word.getText().toString().length();
					weibo_wordnum.setText(numNew + limit);
				}				
				
			}
		});
		weibo_word.requestFocus();
	}
	
	private void init(){
		
		
		weibo_word.setText(weibo_word.getText().toString() +tip);
		weibo_word.findFocus();
//		Uri uri = this.getIntent().getData();
//		try {
//			RequestToken requestToken = OAuthConstant.getInstance()
//					.getRequestToken();
//			AccessToken accessToken = requestToken.getAccessToken(uri
//					.getQueryParameter("oauth_verifier"));
//			OAuthConstant.getInstance().setAccessToken(accessToken);
//		} catch (WeiboException e) {
//			e.printStackTrace();
//		}
	}
	
	private void send(){
	
		String cotent = weibo_word.getText().toString();		
		Bundle b=new Bundle();
		b.putString(WeiBoUtil.WEIBO_TEXT, cotent);
		b.putString(WeiBoUtil.WEIBO_PIC, picpath);
		
		WeiBoUtil.getInstance().shareWeibo(ShareWeiBo.this, null, b, true);
		
	}
	
	public static byte[] readFileImage(String filename) throws IOException {
		BufferedInputStream bufferedInputStream = new BufferedInputStream(
				new FileInputStream(filename));
		int len = bufferedInputStream.available();
		byte[] bytes = new byte[len];
		int r = bufferedInputStream.read(bytes);
		if (len != r) {
			bytes = null;
			throw new IOException("读取文件不正确");
		}
		bufferedInputStream.close();
		return bytes;
	}
}
